package com.module.my.view

import android.view.View
import com.module.app.base.BaseActivity
import com.module.app.pop.CommonPop
import com.module.app.utils.AutoSkinNightUtils
import com.module.app.utils.MySkinUtils
import com.module.app.utils.UserUtils
import com.module.app.bean.IType.IAutoNight
import com.module.base.utils.DisplayUtils
import com.module.base.utils.TimeUtils
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.module.frame.ext.postEvent
import com.module.my.R
import com.module.my.databinding.MyActivityAutoSkinNightBinding
import com.module.my.ext.autoNightFlow
import com.module.my.model.AutoSkinNightViewModel
import com.module.my.utils.startVip
import java.util.*

class AutoSkinNightActivity :
    BaseActivity<AutoSkinNightViewModel, MyActivityAutoSkinNightBinding>() {

    val calendar: Calendar by lazy {
        Calendar.getInstance()
    }
    val calendarEnd: Calendar by lazy {
        Calendar.getInstance()
    }

    override fun bingViewModel() {
        mDatabind.vm = mViewModel
        mViewModel.context = getContext()
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.my_skin_night_title)
        setType(com.module.app.utils.AutoSkinNightUtils.getType())

        //使用今天时间相加，只是用具体日期来算时分秒，比较好算，而且TimePickerView是使用具体日期
        calendar.timeInMillis = TimeUtils.getToday() + com.module.app.utils.AutoSkinNightUtils.getStartTime()
        calendarEnd.timeInMillis = TimeUtils.getToday() + com.module.app.utils.AutoSkinNightUtils.getEndTime()
        //当2者存在相同，正常是第一次使用
        if (calendar.timeInMillis == calendarEnd.timeInMillis) {
            //设置晚上10点
            calendar.timeInMillis = TimeUtils.getToday() + 1000 * 60 * 60 * 22
            //设置凌晨6点
            calendarEnd.timeInMillis = TimeUtils.getToday() + 1000 * 60 * 60 * 6
        }
        setStartTime(calendar.timeInMillis)
        setEndTime(calendarEnd.timeInMillis)
    }

    private fun setType(@IAutoNight type: Int) {
        when (type) {
            IAutoNight.TYPE_NO -> {
                mDatabind.ivSelectNo.setImageResource(R.drawable.app_ic_checkbox_select_2)
                mDatabind.ivSelectSystem.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.ivSelectTime.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.vMask.visibility = View.VISIBLE
                mDatabind.layoutTimeSelect.alpha = 0.3f
            }
            IAutoNight.TYPE_SYSTEM -> {
                mDatabind.ivSelectNo.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.ivSelectSystem.setImageResource(R.drawable.app_ic_checkbox_select_2)
                mDatabind.ivSelectTime.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.vMask.visibility = View.VISIBLE
                mDatabind.layoutTimeSelect.alpha = 0.3f
            }
            IAutoNight.TYPE_TIME -> {
                mDatabind.ivSelectNo.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.ivSelectSystem.setImageResource(R.drawable.app_ic_checkbox2)
                mDatabind.ivSelectTime.setImageResource(R.drawable.app_ic_checkbox_select_2)
                mDatabind.vMask.visibility = View.GONE
                mDatabind.layoutTimeSelect.alpha = 1f
            }
        }
    }

    fun onClickNo(view: View) {
        //设置主题不能马上设置图片，不然会闪退，所以setType 要放在AutoSkinNightUtils.setType 前面
        val type = IAutoNight.TYPE_NO
        setType(type)
        com.module.app.utils.AutoSkinNightUtils.setType(type)
        postEvent(autoNightFlow, true)
    }

    fun onClickSystem(view: View) {
        if (!com.module.app.utils.MySkinUtils.isNight()) {
            com.module.app.pop.CommonPop.Builder(getContext())
                .setContent(R.string.my_skin_night_no_dowanload)
                .setCancelButton(R.string.cancel)
                .setConfirmButton(R.string.download)
                .setOnConfirmListener { mViewModel.getSkinNight() }
                .show()
            return
        }
        //设置主题不能马上设置图片，不然会闪退，所以setType 要放在AutoSkinNightUtils.setType 前面
        val type = IAutoNight.TYPE_SYSTEM
        setType(type)
        com.module.app.utils.AutoSkinNightUtils.setType(type)
        postEvent(autoNightFlow, true)
    }

    fun onClickTime(view: View) {
        if (com.module.app.utils.UserUtils.isVip()) {
            if (!com.module.app.utils.MySkinUtils.isNight()) {
                com.module.app.pop.CommonPop.Builder(getContext())
                    .setContent(R.string.my_skin_night_no_dowanload)
                    .setCancelButton(R.string.cancel)
                    .setConfirmButton(R.string.download)
                    .setOnConfirmListener { mViewModel.getSkinNight() }
                    .show()
                return
            }
            //设置主题不能马上设置图片，不然会闪退，所以setType 要放在AutoSkinNightUtils.setType 前面
            val type = IAutoNight.TYPE_TIME
            setType(type)
            com.module.app.utils.AutoSkinNightUtils.setType(type)
            postEvent(autoNightFlow, true)
        } else {
            com.module.app.pop.CommonPop.Builder(getContext())
                .setContent(R.string.my_vip_content)
                .setCancelButton(R.string.cancel)
                .setConfirmButton(R.string.buy_vip)
                .setOnConfirmListener { getContext().startVip() }
                .show()
        }
    }


    fun onClickStartTime(view: View) {
        val pvTime = TimePickerBuilder(
            getContext()
        ) { date, v -> setStartTime(date.time) }
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .setLabel("年", "月", "日", "点", "分", "秒") //默认设置为年月日时分秒
            .setLineSpacingMultiplier(2.2f)
            .setItemVisibleCount(7)
            .setContentTextSize(16) //滚轮文字大小
            .setSubCalSize(16)
            .setDate(calendar)
            .setSubmitColor(resources.getColor(R.color.bg_default)) //确定按钮文字颜色
            .setCancelColor(resources.getColor(R.color.color_text_sub2)) //取消按钮文字颜色
            .setDividerColor(resources.getColor(R.color.color_line))
            .build()
        pvTime.findViewById(R.id.timepicker)
            .setPadding(0, DisplayUtils.dip2px(10f), 0, DisplayUtils.dip2px(10f))
        pvTime.show()
    }

    fun onClickEndTime(view: View) {
        val pvTime = TimePickerBuilder(
            getContext()
        ) { date, v -> setEndTime(date.time) }
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .setLabel("年", "月", "日", "点", "分", "秒") //默认设置为年月日时分秒
            .setLineSpacingMultiplier(2.2f)
            .setItemVisibleCount(7)
            .setContentTextSize(16) //滚轮文字大小
            .setSubCalSize(16)
            .setDate(calendarEnd)
            .setSubmitColor(resources.getColor(R.color.bg_default)) //确定按钮文字颜色
            .setCancelColor(resources.getColor(R.color.color_text_sub2)) //取消按钮文字颜色
            .setDividerColor(resources.getColor(R.color.color_line))
            .build()
        pvTime.findViewById(R.id.timepicker)
            .setPadding(0, DisplayUtils.dip2px(10f), 0, DisplayUtils.dip2px(10f))
        pvTime.show()
    }

    /**
     * 设置开始时间
     *
     * @param time 单位秒
     */
    fun setStartTime(time: Long) {
        if (time == 0L) {
            return
        }
        calendar.timeInMillis = TimeUtils.getMillSecond(time)
        val hours = calendar[Calendar.HOUR_OF_DAY].toLong()
        val minutes = calendar[Calendar.MINUTE].toLong()
        val isSuc = com.module.app.utils.AutoSkinNightUtils.setStartTime(hours, minutes)
        if (isSuc) {
            mDatabind.tvStartTime.text = TimeUtils.getDate(time, "HH:mm")
        }
    }

    /**
     * 设置结束时间
     *
     * @param time 单位秒
     */
    fun setEndTime(time: Long) {
        if (time == 0L) {
            return
        }
        calendarEnd.timeInMillis = TimeUtils.getMillSecond(time)
        val hours = calendarEnd[Calendar.HOUR_OF_DAY].toLong()
        val minutes = calendarEnd[Calendar.MINUTE].toLong()
        val isSuc = com.module.app.utils.AutoSkinNightUtils.setEndTime(hours, minutes)
        if (isSuc) {
            mDatabind.tvEndTime.text = TimeUtils.getDate(time, "HH:mm")
        }
    }
}